package org.example.judge_admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.judge_admin.mapper.UserAuthMapper;
import org.example.judge_admin.service.AuthService;
import org.example.judge_admin.service.UserAuthService;
import org.example.judge_common.util.IdWorker;
import org.example.judge_pojo.Auth;
import org.example.judge_pojo.UserAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("UserAuthServiceImpl")
@Transactional
public class UserAuthServiceImpl extends ServiceImpl<UserAuthMapper, UserAuth> implements UserAuthService {

    @Autowired
    private IdWorker idWorker;


    @Qualifier("AuthServiceImpl")
    @Autowired
    private AuthService authService;
    @Override
    public boolean save(UserAuth entity) {
        entity.setId(idWorker.nextId().toString());
        entity.setCreateTime(new Date());
        return super.save(entity);
    }


    @Override
    public void updateAuthByUserId(String userId, List<String> auths,Integer status) {
        if(auths.size()==0&&status==2){
            //用户执行了删除用户的所有权限
            log.warn("用户不能删除所有权限。必须保留一个Client权限");
            if(auths==null)auths = new ArrayList<>();
            auths.add(authService.getAuthByCode(Auth.CLIENT).getId());
        }
        //将另一种类型的权限全加，防删
        List<Auth> others = this.authService.listByUserId(userId, status == 2 ? 0 : 2,true);

        for (int i = 0; i < others.size(); i++) {
            System.out.println(auths);
            System.out.println(auths.add(others.get(i).getId()));
        }
        //删除不存在的ids
        QueryWrapper<UserAuth> query = new QueryWrapper<>();
        query.eq("user_id",userId);
        query.notIn("auth_id",auths);
        this.remove(query);
        //获取所有角色权限
        QueryWrapper<UserAuth> userAuthQueryWrapper = new QueryWrapper<>();

        userAuthQueryWrapper.eq("user_id",userId).in("auth_id",auths);
        List<UserAuth> list = this.list(userAuthQueryWrapper);
        //添加存在的ids
        List<UserAuth> inserts = new ArrayList<>();
        for (String auth : auths) {
            boolean flag = false;   //false表示需要新加的auth，true表示已有，不需要添加
            for (UserAuth userAuth : list) {
                if(userAuth.getAuthId().equals(auth)){
                    flag = true;
                    break;
                }
            }
            if(!flag){ //在 list 中不存在新的auths，加入数据库
                UserAuth userAuth = new UserAuth();
                userAuth.setId(idWorker.nextId().toString());
                userAuth.setUserId(userId);
                userAuth.setAuthId(auth);
                userAuth.setCreateTime(new Date());
                inserts.add(userAuth);
            }

        }
        //添加数据库
        if(inserts.size()>0)this.saveBatch(inserts);
    }

    @Override
    public boolean removeByUserId(Serializable id) {
        return super.remove(new QueryWrapper<UserAuth>().eq("user_id",id));
    }

    @Override
    public boolean removeByAuthId(Serializable id) {
        return super.remove(new QueryWrapper<UserAuth>().eq("auth_id",id));
    }
}
